<template>
  <div>
    <BasicTable @register="registerTable" @fetch-success="onFetchSuccess">
      <template #toolbar>
        <a-button type="primary" @click="handleCreate"> {{ t('sys.menu.addMenu') }} </a-button>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction
            :actions="[
              {
                label: t('sys.configuration.edit'),
                onClick: handleEdit.bind(null, record),
              },
              {
                label: t('sys.configuration.delete'),
                color: 'error',
                popConfirm: {
                  title: t('sys.configuration.IsDelete'),
                  placement: 'left',
                  confirm: handleDelete.bind(null, record),
                },
              },
              {
                label: t('sys.menu.addTwoCatalog'),
                onClick: handleCreateSubMenu.bind(null, record),
              },
            ]"
          />
        </template>
      </template>
    </BasicTable>
    <MenuDrawer @register="registerDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts" setup>
  import { nextTick } from 'vue';
  import { BasicTable, useTable, TableAction } from '@/components/Table';
  // 由于 AntdVTable 已声明但未使用，移除该导入语句
  import { getMenuList, deleteMenu } from '@/api/demo/system';
  import { useDrawer } from '@/components/Drawer';
  import MenuDrawer from './MenuDrawer.vue';
  import { columns, searchFormSchema } from './menu.data';
  import { useI18n } from '@/hooks/web/useI18n';

  defineOptions({ name: 'MenuManagement' });

  const { t } = useI18n();
  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, expandAll }] = useTable({
    title: t('sys.menu.menuTitle'),
    api: getMenuList,
    columns,
    formConfig: {
      labelWidth: 120,
      schemas: searchFormSchema,
    },
    pagination: true,
    striped: false,
    useSearchForm: true,
    showTableSetting: true,
    bordered: true,
    showIndexColumn: false,
    canResize: false,
    actionColumn: {
      width: 50, // 增加宽度以适应新按钮
      title: t('sys.configuration.action'),
      dataIndex: 'action',
      fixed: undefined,
    },
  });
  function handleCreate(record: Recordable) {
    openDrawer(true, {
      isUpdate: false,
      record,
    });
  }

  function handleCreateSubMenu(record: Recordable) {
    openDrawer(true, {
      record,
      isUpdate: false,
      isSubMenu: true, // 新增标识
    });
  }

  function handleEdit(record: Recordable) {
    openDrawer(true, {
      record,
      isUpdate: true,
    });
  }

  async function handleDelete(record: Recordable) {
    try {
      await deleteMenu(record.permissionId); // 调用删除菜单的 API
      reload(); // 重新加载表格数据
    } catch (error) {
      console.error('删除菜单失败:', error);
    }
  }

  function handleSuccess() {
    reload();
  }

  function onFetchSuccess() {
    // 演示默认展开所有表项
    nextTick(expandAll);
  }
</script>
